// miniprogram/pages/shudu/shudu.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    key: '',
    difficulty: 'normal',
    puzzle: [
      [0,3,8,7,6,2,0,5,1],
      [1,0,0,3,8,4,9,0,2],
      [4,7,2,1,9,5,0,0,6],
      [0,1,0,0,0,0,8,0,0],
      [0,0,0,0,0,1,0,0,9],
      [6,2,0,8,0,0,1,3,7],
      [2,4,1,0,7,3,6,9,0],
      [8,9,5,4,2,6,7,1,3],
      [0,6,3,9,1,8,2,0,5]
  ],
  solution: [
      [9,3,8,7,6,2,4,5,1],
      [1,5,6,3,8,4,9,7,2],
      [4,7,2,1,9,5,3,8,6],
      [5,1,9,6,3,7,8,2,4],
      [3,8,7,2,4,1,5,6,9],
      [6,2,4,8,5,9,1,3,7],
      [2,4,1,5,7,3,6,9,8],
      [8,9,5,4,2,6,7,1,3],
      [7,6,3,9,1,8,2,4,5]
  ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getKey()
  },
    

getKey() {
  let that = this
  wx.cloud.callFunction({
    name: 'getKey',
    data: {
      name: 'shudu'
    }
  }).then(res => {
    console.log(res.result.data[0].key)
    that.setData({
      key: res.result.data[0].key
      
    })
    this.getData()
  })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inputnum(event){
    // console.log(event.detail.value)
    var i=event.currentTarget.dataset.i
    var j=event.currentTarget.dataset.j
    var that=this
    this.data.puzzle[i][j]=parseInt(event.detail.value)
    // console.log(index2)
  },
  onInput(){
    // console.log(this.data.puzzle)   
    var that=this
    var flag=true
    for(var i=0;i<that.data.puzzle.length;i++){
        for(var j=0;j<that.data.puzzle[i].length;j++){
            if(that.data.puzzle[i][j]!=that.data.solution[i][j]){
                that.data.puzzle[i][j]=-1
                flag=false
            }
        }
    }
    // console.log(this.data.solution)   
    if(flag){
        wx.showModal({
            content: '恭喜您挑战成功',
            showCancel: false,
          })
    }
    else{
        wx.showModal({
            content: '挑战失败，再接再厉哦',
            showCancel: false,
          })
    }
  },
  change(e){
    this.setData({
        difficulty: e.currentTarget.dataset.difficulty
    })
    this.getData()

  },
  getData() {
    var that = this;
      wx.request({
        url: "https://apis.juhe.cn/fapig/sudoku/generate?key="+that.data.key+"&difficulty="+that.data.difficulty,
        data: {
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res)
          wx.hideLoading();
          if (res.statusCode == 200) {
            that.setData({
              puzzle: res.data.result.puzzle,
              solution: res.data.result.solution
            })
          } else {
            that.setData({
              result: '服务器错误！',
              show: false
            })
          }
        },
        fail(res){
          console.log(res)
        }
      })
    
  },
})